<?php

/**
* alltosun.com 评论控制器 index.php
* ============================================================================
* 版权所有 (C) 2009-2014 北京互动阳光科技有限公司，并保留所有权利。
* 网站地址: http://www.alltosun.com
* ----------------------------------------------------------------------------
* 许可声明: 这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
* ============================================================================
* $Author: 吴松键 (wusj@alltosun.com) $
* $Date: 2014-9-16 下午5:19:55 $
* $Id$
*/

class Action
{
    private $user_id;
    private $per_page = 6;

    public function __construct()
    {
        $this->user_id = user_helper::get_user_id();
        if (empty($this->user_id)) {
            Response::redirect(AnUrl('index'));
        }
    }

    public function __call($action = '', $param = array())
    {
        $goods_id = $action;
        $page = Request::Get('page_no', 1);

        $goods_info = _uri('goods', $goods_id);

        if (!$goods_info) {
            return '该商品不存在或被删除';
        }

        $per_page = 10;

        $count = _model('comment')->getTotal(array('goods_id'=>$goods_id));

        $comment_list = array();
        if ($count) {
            $pager = new Pager($per_page);
            if ($pager->generate($count)) {
                Response::assign('pager', $pager);
            }
            $comment_list = _model('comment')->getList(array('goods_id'=>$goods_id), ' ORDER BY `add_time` DESC '.$pager->getLimit($page));
        }

        Response::assign('goods_info', $goods_info);
        Response::assign('comment_list', $comment_list);
        Response::display('comment_list.html');
    }

    public function index()
    {
        $page = Request::Get('page_no', 1);
        $status = Request::Get('status', 0);
        $user_id = $this->user_id;
        $order_ids = _model('order')->getFields('id', array('user_id'=>$user_id, 'progress'=>20));

        if ($order_ids) {
            $ids = _model('order_relation')->getFields('id', array('order_id'=>$order_ids));
            $tmp = _model('comment')->getFields('order_relation_id', array('user_id'=>$user_id));

            if (is_mobile()) {
                $no_order_ids = array_diff($ids, $tmp);
                $yew_order_ids = $tmp;
                if (!$no_order_ids) {
                    $no_order_ids = '';
                }
                if (!$yew_order_ids) {
                    $yew_order_ids = '';
                }
                $no_order_list = _model('order_relation')->getList(array('id'=>$no_order_ids));
                $yes_order_list = _model('order_relation')->getList(array('id'=>$yew_order_ids));
                $order_relation_list = array();
                $order_relation_list = array_merge($no_order_list, $yes_order_list);
            } else {
                if ($status == 1) {
                    $order_relation_ids = $tmp;
                } else {
                    $order_relation_ids = array_diff($ids, $tmp);
                }
                $goods_info = array();
                foreach ($order_relation_ids as $k=>$v) {
                    $goods_id = _uri('order_relation', $v, 'goods_id');
                    $goods_info = _uri('goods', $goods_id);

                    if (!empty($goods_info['flagship']) && !empty($goods_info['flagship_link'])) {
                        unset($order_relation_ids[$k]);
                    }
                }
                if (!$order_relation_ids) {
                    $order_relation_ids = '';
                }
                $count = _model('order_relation')->getTotal(array('id'=>$order_relation_ids));

                $order_relation_list = array();
                if ($count) {
                    $pager = new Pager($this->per_page);
                    if ($pager->generate($count)) {
                        Response::assign('pager', $pager);
                    }
                    $order_relation_list = _model('order_relation')->getList(array('id'=>$order_relation_ids), $pager->getLimit($page));
                }
            }
            if (!empty($order_relation_list)) {
                foreach ($order_relation_list as $k=>$v) {
                    $goods_info = array();
                    $goods_id = _uri('order_relation', $v['id'], 'goods_id');
                    $goods_info = _uri('goods', $goods_id);
                    if (!empty($goods_info['flagship']) && !empty($goods_info['flagship_link'])) {
                        unset($order_relation_list[$k]);
                    } else {
                        $order_relation_list[$k]['count'] = _model('comment')->getTotal(array('goods_id'=>$v['goods_id']));
                        $order_relation_list[$k]['goods_info'] = _uri('goods', $v['goods_id']);
                        $order_relation_list[$k]['comment_info'] = _uri('comment', array('order_relation_id'=>$v['id'], 'user_id'=>$user_id));
                    }
                }
            }
            Response::assign('order_relation_list', $order_relation_list);
        }

        Response::assign('status', $status);
        display_template('comment.html');
    }


    public function add()
    {
        $order_relation_id = Request::Get('order_relation_id', 0);
        $comment_id = Request::Get('comment_id', 0);

        $comment_info = array();
        $order_info = array();
        $order_relation_info = array();
        $label = array();
        $order_relation_info = _uri('order_relation', $order_relation_id);

        if (!$order_relation_info) {
            return '该商品订单不存在';
        }
        $order_info = _uri('order', $order_relation_info['order_id']);

        $user_id = user_helper::get_user_id();

        if ($user_id != $order_info['user_id']) {
            return '你不能操作别人的评论';
        }
        $sku_snapshot_info = _uri('sku_snapshot', $order_relation_info['sku_snapshot_id']);

        if ($comment_id) {
            $comment_info = _uri('comment', $comment_id);
            if (!$comment_info) {
                return '该评论不存在';
            }

            $label = _model('comment_relation')->getFields('comment_label_id', array('comment_id'=>$comment_id));
            Response::assign('comment_id', $comment_id);
            Response::assign('comment_info', $comment_info);
        }

        Response::assign('label', $label);
        Response::assign('sku_snapshot_info', $sku_snapshot_info);
        Response::assign('order_info', $order_info);
        Response::assign('order_relation_info', $order_relation_info);
        display_template('add.html');
    }

    public function save()
    {
        $user_id = $this->user_id;
        $order_relation_id = Request::Post('order_relation_id', 0);
        $star = Request::Post('star', 0);
        $label = Request::Post('label', array());
        $title = Request::Post('title', '');
        $img = Request::Post('image', array());
        $comment_id = Request::Post('comment_id', 0);
        $order_relation_info = _uri('order_relation', $order_relation_id);

        if (!$order_relation_info) {
            return '该订单商品不存在';
        }

        if (!$star) {
            return '请选择评价星级';
        }

        //zhangyh add 如果是手机则不判断
        if (!$label && !is_mobile()) {
            return '请选择评论标签';
        }

        if (!$title) {
            return '请填写评论内容';
        }

        $old_info = _uri('comment', array('user_id'=>$user_id, 'order_relation_id'=>$order_relation_id));

        if ($old_info && !$comment_id) {
            return '已经评论过不能在评论';
        }

        if ($comment_id) {
            $filter = array(
                    'order_relation_id'=>$order_relation_id,
                    'star'=>$star,
                    'title'=>$title,
                    'user_id'=>$user_id,
                    'status'=>1,
                    'goods_id'=>$order_relation_info['goods_id'],
            );
            _model('comment')->update($comment_id, $filter);

            if ($img) {
                $old_img = _model('attachment_relation')->getFields('attachment_id', array('res_name'=>'comment', 'res_id'=>$comment_id));
                $del_img = array_diff($old_img, $img);
                $add_img = array_diff($img, $old_img);
                foreach ($del_img as $k=>$v) {
                    attachment_helper::del_attachment_relation('comment', $comment_id, $v);
                }

                foreach ($add_img as $k=>$v) {
                    attachment_helper::add_attachment_relation('comment', $comment_id, $v);
                }
            } else {
                _model('attachment_relation')->delete(array('res_name'=>'comment', 'res_id'=>$comment_id));
            }


            $old_label = _model('comment_relation')->getFields('comment_label_id', array('comment_id'=>$comment_id));

            $create_label = array_diff($label, $old_label);
            $del_label = array_diff($old_label, $label);

            foreach ($create_label as $k=>$v) {
                if (is_numeric($v)) {
                    $label_id = $v;
                } else {
                    $label_id = _model('comment_label')->create(array('title'=>$v));
                }
                _model('comment_relation')->create(array('comment_id'=>$comment_id, 'comment_label_id'=>$label_id));
            }
            foreach ($del_label as $k=>$v) {
                _model('comment_relation')->delete(array('comment_label_id'=>$v));
            }

        } else {
            $filter = array(
                    'order_relation_id'=>$order_relation_id,
                    'star'=>$star,
                    'title'=>$title,
                    'user_id'=>$user_id,
                    'status'=>1,
                    'goods_id'=>$order_relation_info['goods_id'],
            );

            $id = _model('comment')->create($filter);

            if ($img) {
                foreach ($img as $k=>$v) {
                    $tmp  = attachment_helper::add_attachment_relation('comment', $id, $v);
                }
            }
            foreach ($label as $k=>$v) {
                if (is_numeric($v)) {

                    $label_id = $v;
                } else {
                    $label_id = _model('comment_label')->create(array('title'=>$v));
                }
                _model('comment_relation')->create(array('comment_id'=>$id, 'comment_label_id'=>$label_id));
            }
        }
        if(is_mobile()) {
            Response::redirect(AnUrl('comment'));
        } else {
            Response::redirect(AnUrl('comment?status=1'));
        }
    }

    public function loadData()
    {
        $params['page_no']  = Request::getParam('page_no', 1);
        $params['goods_id'] = Request::getParam('goods_id', 0);

        $list = _widget('comment')->get_list($params);
        Response::assign('list', $list);

        $html = Response::fetch(MODULE_PATH.'/comment/template/widget/comment_list.html');

        return array('info'=>'ok', 'data'=>$html);
    }

    public function comment_user()
    {
        $user_id = Request::Post('user_id', 0);
        $comment_id = Request::Post('comment_id', 0);
        $comment_status = Request::Post('comment_status', 0);

        if (!$user_id) {
            return false;
        }

        if (!$comment_id) {
            return '请选择标签';
        }

        if (!$comment_status) {
            return '请正确操作';
        }

        $info = _uri('comment_user', array('user_id'=>$user_id, 'comment_id'=>$comment_id));

        if ($info) {
            return '已经操作过';
        }

        _model('comment_user')->create(array('user_id'=>$user_id, 'comment_id'=>$comment_id, 'comment_status'=>$comment_status));

        return 'ok';
    }
}
?>